package com.mida.app.knw.service.impl;


import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.mida.app.knw.service.DetailCodingSystemService;
import com.mida.app.knw.service.KnwNoumenonRelationService;
import com.mida.dms.knw.api.KnwNoumenonRelationApi;
import com.mida.dms.knw.entity.vo.KnwNoumenonRelationVo;
import com.mida.dms.knw.entity.vo.KnwNoumenonVo;
import com.mida.dms.knw.entity.vo.MapperPage;
import com.mida.dms.knw.vo.KnwNoumenonRelationPageVo;

import com.mida.dms.knw.vo.PageResultVo;
import com.mida.dms.knw.vo.PullDownMenuOfNoumenonVO;
import com.mida.dms.mdm.pojo.auto.MsdDataSet;
import com.mida.tool.common.result.ActionResult;
import com.mida.tool.common.result.ListResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.time.StopWatch;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.mida.tool.common.result.BaseResult.buildListSuccess;
import static com.mida.tool.common.result.BaseResult.buildListWarn;

/**
 * <h1>本体关系服务实现 </h1>
 * <p>
 *
 * </p>
 *
 * @author jian.li on 2021/6/22 11:35
 */
@Slf4j
@Service
public class KnwNoumenonRelationServiceImpl implements KnwNoumenonRelationService {

    @Autowired
    private KnwNoumenonRelationApi knwNoumenonRelationApi;
    @Autowired
    private DetailCodingSystemService detailCodingSystemService;

    @Override
    public ActionResult add(KnwNoumenonRelationVo relation) {
        return knwNoumenonRelationApi.add(relation);
    }

    @Override
    public ListResult<List<PullDownMenuOfNoumenonVO>> findSourceAndTarget(MapperPage mapperPage) {
        log.info("源本体目标本体下拉查询: " + mapperPage.toString());
        StopWatch st = StopWatch.createStarted();
        ListResult result = new ListResult();
        try {
            ListResult<List<KnwNoumenonVo>> sourceAndTarget = knwNoumenonRelationApi.findSourceAndTarget(mapperPage);
            List<KnwNoumenonVo> knwNoumenonVos = sourceAndTarget.getData();
            knwNoumenonVos.stream().filter(knwNoumenonVo->ObjectUtils.isNotEmpty(knwNoumenonVo)).forEach(knwNoumenonVo -> {
                Long noumenonSubType = knwNoumenonVo.getNoumenonSubType();
                MsdDataSet conceptSoidByDataSetSoid = detailCodingSystemService.getConceptSoidByDataSetSoid(noumenonSubType);
                String dataSetMeaning = conceptSoidByDataSetSoid.getDataSetMeaning();
                knwNoumenonVo.setNoumenonSubTypeName(dataSetMeaning);
            });

            List<PullDownMenuOfNoumenonVO> targetList = new ArrayList<>();
            targetList = knwNoumenonVos
                    .stream()
                    .filter(knwNoumenonVo -> ObjectUtils.isNotEmpty(knwNoumenonVo))
                    .map(noumenon -> new PullDownMenuOfNoumenonVO(noumenon.getNoumenonSoid(), noumenon.getNoumenonShowName(), noumenon.getNoumenonSubTypeName(), null))
                    .collect(Collectors.toList());
            buildListSuccess(result, targetList);
        } catch (Exception e) {
            log.error("源本体目标本体下拉查询异常: ", e);
            buildListWarn(result, "源本体目标本体下拉查询失败");
        } finally {
            st.stop();
            log.info("本次查询耗时:" + st.getTime() + "ms");
        }
        return result;

    }

    @Override
    public ListResult<PageResultVo<KnwNoumenonRelationVo>> findPage(KnwNoumenonRelationPageVo vo) {
        log.info("开始本体关系分页查询,查询条件: " + vo.toString());
        ListResult result = new ListResult();
        StopWatch stopWatch = StopWatch.createStarted();

        ListResult<PageResultVo<KnwNoumenonRelationVo>> page = null;
        try {
            page = knwNoumenonRelationApi.findPage(vo);
            if (ObjectUtils.isNotEmpty(page) && HttpStatus.OK.value() == page.getStatus()) {
                PageResultVo<KnwNoumenonRelationVo> data = page.getData();
                if (ObjectUtils.isNotEmpty(data)) {
                    List<KnwNoumenonRelationVo> list = data.getList();
                    if (CollectionUtils.isNotEmpty(list)) {
                        List<Long> sourceCollect = list.stream().filter(e -> ObjectUtils.isNotEmpty(e)).map(k -> k.getSourceNoumenonSubType()).collect(Collectors.toList());
                        if (CollectionUtils.isNotEmpty(sourceCollect)) {
                            Map<Long, String> map = detailCodingSystemService.getDataSetNameMap(sourceCollect);
                            if (MapUtils.isNotEmpty(map)) {
                                list.forEach(k -> k.setSourceNoumenonSubTypeName(map.get(k.getSourceNoumenonSubType())));
                            }
                        }
                        List<Long> targetCollect = list.stream().filter(e -> ObjectUtils.isNotEmpty(e)).map(k -> k.getTargetNoumenonSubType()).collect(Collectors.toList());
                        if (CollectionUtils.isNotEmpty(targetCollect)) {
                            Map<Long, String> map = detailCodingSystemService.getDataSetNameMap(targetCollect);
                            if (MapUtils.isNotEmpty(map)) {
                                list.forEach(k -> k.setTargetNoumenonSubTypeName(map.get(k.getTargetNoumenonSubType())));
                            }
                        }
                    }
                }
            }
        } catch (Exception e) {
            log.error("分页查询本体关系发送异常");
            buildListWarn(result, "分页查询本体关系发送失败");
        } finally {
            stopWatch.stop();
            log.info("本次分页查询耗时: " + stopWatch.getTime() + "ms");
        }
        return page;
    }

    @Override
    public ActionResult enable(Long soid) {
        return knwNoumenonRelationApi.enable(soid);
    }

    @Override
    public ListResult<Object> findRelation(String searchData) {
        return knwNoumenonRelationApi.findRelation(searchData);
    }

    @Override
    public ActionResult updateSingle(KnwNoumenonRelationVo relation) {
        return knwNoumenonRelationApi.updateSingle(relation);
    }

    @Override
    public ActionResult delete(Long soid) {
        return knwNoumenonRelationApi.delete(soid);
    }
}
